using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace gears
{
    class Animation
    {
        private Texture2D animation_sprite;
        int current_frame = 1;
        int current_row = 0;
        int sprite_width = 0;
        int sprite_height = 0;
        int frame_count = 0;
        int row_count = 1;
        long prev_time = 0;
        long time = 0;
        float fps = 0;
        Rectangle animation_rectangle;

        public Animation(Texture2D _sprite,int _frame_count,int _row_count,int frame_per_sec)
        {
            animation_sprite = _sprite;
            sprite_width = animation_sprite.Width/_frame_count;
            sprite_height = animation_sprite.Height / _row_count;
            row_count = _row_count;
            frame_count = _frame_count;
            fps = 1000 / frame_per_sec;

        }
       
        public void Animating()
        {
            animation_rectangle = new Rectangle(current_frame * sprite_width, 0, sprite_width, sprite_height);

            time = Convert.ToInt64(DateTime.Now.ToString("0MMddyyHmmssfff"));

            if (time-prev_time > fps)
            {
                if (current_frame < frame_count-1)
                {
                    current_frame++;
                }
                else
                {
                    current_frame = 0;
                }
                prev_time = time;
            }
            animation_rectangle.X = current_frame * sprite_width;
            animation_rectangle.Y = sprite_height * current_row;
        }
        public void DrawAnimation(SpriteBatch sprite_batch,Vector2 animation_position)
        {
            sprite_batch.Draw(animation_sprite, animation_position, animation_rectangle, Color.White);
        }
        public void DrawScalableAnimation(SpriteBatch sprite_batch, Vector2 animation_position,float scale, float alpha)
        {
            sprite_batch.Draw(animation_sprite, new Rectangle((int)animation_position.X,(int)animation_position.Y,(int)(sprite_width*scale),(int)(sprite_height*scale)), animation_rectangle, Color.White*alpha);
        }
        public void DrawScalableAndRotatableAnimation(SpriteBatch sprite_batch, Vector2 animation_position,float rotation, Vector2 scale, float alpha)
        {
            sprite_batch.Draw(animation_sprite,
                                 new Rectangle((int)(animation_position.X), (int)(animation_position.Y), (int)scale.X, (int)scale.Y),
                                 animation_rectangle,
                                 Color.White,
                                 rotation,
                                 new Vector2(0, 0),
                                 SpriteEffects.None,
                                 0.0f);
        }
        public int SpriteWidth
        {
            get
            {
                return sprite_width;
            }
        }

        public int SpriteHeight
        {
            get
            {
                return sprite_height;
            }
        }

        public int CurrentFrame
        {
            get
            {
                return current_frame;
            }
            set
            {
                current_frame = value;
            }
        }

        public int CurrentRow
        {
            get
            {
                return current_row;
            }
            set
            {
                current_row = value;
            }
        }


    }
}
